package com.jack.payment.service.basic.impl;

import com.jack.payment.constant.ResultHintConstant;
import com.jack.payment.dao.user.UserApiDao;
import com.jack.payment.dao.user.UserDeviceDao;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.filter.UserFilter;
import com.jack.payment.entity.generate.user.UserApiModel;
import com.jack.payment.entity.generate.user.UserDeviceModel;
import com.jack.payment.util.network.IP2RegionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserMessageServiceImpl {
    private Logger log = LoggerFactory.getLogger(UserMessageServiceImpl.class);

    @Autowired
    private UserDeviceDao userDeviceDao;
    @Autowired
    private UserApiDao userApiDao;

    /**
     * 筛选出符合设备条数的总数
     * @param userDeviceFilterModel
     * @return
     */
    public Integer getAppointUserDeviceCount(UserFilter.UserDeviceFilter userDeviceFilterModel) {
        return userDeviceDao.getAppointUserDeviceCount(userDeviceFilterModel);
    }

    /**
     * 筛选出符合条件设备列表
     * @param userDeviceFilterModel
     * @return
     */
    public List<UserDeviceModel> getAppointUserDeviceList(UserFilter.UserDeviceFilter userDeviceFilterModel) throws BusinessException {
        List<UserDeviceModel> resultUserDeviceList = userDeviceDao.getAppointUserBindDeviceList(userDeviceFilterModel);
        if (resultUserDeviceList.isEmpty()) {
            log.warn("筛选的用户设备数为空");
            throw new BusinessException(ResultHintConstant.DATA_NOT_EXIST);
        }
        resultUserDeviceList = resultUserDeviceList.stream().map(
                item -> {
                    item.setIpAddress(IP2RegionUtil.getAppointIpRegion(item.getIpAddress()));
                    return item;
                }
        ).collect(Collectors.toList());
        return resultUserDeviceList;
    }

    /**
     * 筛选出符合api的总数
     * @param userApiFilterModel
     * @return
     */
    public Integer getUserApiModelCount(UserFilter.UserApiFilter userApiFilterModel) {
        return userApiDao.getAppointUserApiCount(userApiFilterModel);
    }

    /**
     * 筛选出符合api 的列表
     * @param userApiFilterModel
     * @return
     */
    public List<UserApiModel> getUserApiModelList(UserFilter.UserApiFilter userApiFilterModel) throws BusinessException {
        List<UserApiModel> resultUserApiList = userApiDao.getAppointUserApiList(userApiFilterModel);
        if (resultUserApiList.isEmpty()) {
            log.warn("筛选出用户设置的api为空");
            throw new BusinessException(ResultHintConstant.DATA_NOT_EXIST);
        }
        return resultUserApiList;
    }
}
